// ActionScript file
import com.founder.framework.common.FounderUtils;
import com.founder.framework.common.StringUtils;
import com.qjz.common.CommonUtils;
import com.qjz.controller.SchoolInfoController;
import com.qjz.controller.StudentInfoController;
import com.qjz.entity.DataQueryParameter;
import com.qjz.entity.StudentInfoVo;
import com.qjz.model.LoginUserModel;
import com.qjz.model.SchoolInfoModel;
import com.qjz.model.StudentInfoModel;

import flash.events.DataEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.system.Security;

import mx.collections.ArrayCollection;
import mx.controls.ProgressBar;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;

private var file:FileReference;
private var progressBar:ProgressBar;
[Bindable]
private var fName:String;
[Bindable]
private var model:StudentInfoModel;
private var controller:StudentInfoController;
[Bindable]
private var schoolModel:SchoolInfoModel;
private var schoolCtrl:SchoolInfoController;
private var _instance:LoginUserModel=LoginUserModel.getInstance();

protected function group1_creationCompleteHandler(event:FlexEvent):void
{
	// TODO Auto-generated method stub
	Security.allowDomain("*");
	
	model=new StudentInfoModel();
	model.addEventListener("GET_EXCEL_DATA", getExcelDataHandler);
	controller=new StudentInfoController(model);
	
	file=new FileReference();
	file.addEventListener(ProgressEvent.PROGRESS, onProgress);
	file.addEventListener(Event.SELECT, onSelect);
	file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, completeHandle);
	
	progressBar=new ProgressBar();
	progressBar.width=200;
	
	if(_instance.user.leiXing == 9)
	{
		schoolModel=new SchoolInfoModel();
		schoolCtrl=new SchoolInfoController(schoolModel);
		schoolCtrl.doSearch(schoolModel.schoolInfoQueryParameter);
		ddlSchool.visible=true;
		ddlSchool.includeInLayout=true;
	}
	else
	{
		labSchool.text=_instance.user.xueXiaoMing;
		labSchool.visible=true;
		labSchool.includeInLayout=true;
	}
}

protected function btnSelect_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	var imageTypes:FileFilter=new FileFilter("Images (*.xls)", "*.xls");
	var allTypes:Array=new Array(imageTypes);
	file.browse(allTypes);
}

private function onSelect(e:Event):void
{
	if (Math.round(file.size) > 4000000)
	{
		FounderUtils.alert("上传文件大小不能大于4M", "系统提示", null, null, FounderUtils.faultIcon);
		return;
	}
	txtFilePath.text=file.name;
	fName=CommonUtils.getUID();
	
	var str:String=FounderUtils.getServerUrl("sims") + "/FileUploaded" + "?fName=" + fName;
	var request:URLRequest=new URLRequest(str);
	try
	{
		file.upload(request);
	}
	catch (error:Error)
	{
		
	}
	
	progressBar=PopUpManager.createPopUp(this, ProgressBar, true) as ProgressBar;
	PopUpManager.centerPopUp(progressBar);
}

private function onProgress(e:ProgressEvent):void
{
	var proc:uint=e.bytesLoaded / e.bytesTotal * 100;
	progressBar.setProgress(proc, 100);
	progressBar.label="文件已解析：" + proc + "%";
}

private function completeHandle(event:Event):void
{
	PopUpManager.removePopUp(progressBar);
	var p:DataQueryParameter=new DataQueryParameter();
	p.fileName=fName + file.type;
	if(_instance.user.leiXing == 9)
	{
		p.schoolCode=ddlSchool.selectedItem.sid;
		p.schoolType=ddlSchool.selectedItem.leiXing;
	}
	else
	{
		p.schoolCode=_instance.user.xueXiaoId.toString();
		p.schoolType=_instance.user.leiXing.toString();
	}
	
	controller.getExcelData(p);
}

private function getExcelDataHandler(event:Event):void
{
	var info:String="";
	var str:String="";
	var shengYuan:String="本施教区、区外本市、本省外市、外省、香港、澳门、台湾、外国";
	for each(var student:StudentInfoVo in model.dataList)
	{
		if(StringUtils.isNullOrEmpty(student.xingMing))
		{
			info += "学生姓名不能为空，请检查\r\n";
			continue;
		}
		str="";
		if(StringUtils.isNullOrEmpty(student.shenFenZheng))
			str += "-身份证号";
		/*if(StringUtils.isNullOrEmpty(student.xingBie))
			str += "-性别";
		if(StringUtils.isNullOrEmpty(student.minZu))
			str += "-民族";
		if(StringUtils.isNullOrEmpty(student.ruXueNian))
			str += "-入学年份";
		if(StringUtils.isNullOrEmpty(student.banJi))
			str += "-班级";
		if(StringUtils.isNullOrEmpty(student.xueXiao))
			str += "-学校";
		if(StringUtils.isNullOrEmpty(student.guoJi))
			str += "-国籍";
		if(StringUtils.isNullOrEmpty(student.huJiDiZhi))
			str += "-户籍所在地址";
		if(StringUtils.isNullOrEmpty(student.changZhuDiQu))
			str += "-家庭常住地区";
		/*if(StringUtils.isNullOrEmpty(student.xiangXiDiZhi))
			str += "-家庭常住地详细地址";
		if(StringUtils.isNullOrEmpty(student.xueJiZhuangTai))
			str += "-学籍状态";
		if(StringUtils.isNullOrEmpty(student.shiMinKa))
			str += "-市民卡号";
		if(StringUtils.isNullOrEmpty(student.paiChuSuo))
			str += "-所在派出所";
		if(StringUtils.isNullOrEmpty(student.jiaTingZhuZhi))
			str += "-家庭住址";
		if(StringUtils.isNullOrEmpty(student.shengYuanXingZhi))
			str += "-生源性质";
		else if(!StringUtils.contains(shengYuan, student.shengYuanXingZhi))
			str += "-生源性质（信息有误）";		
		if(StringUtils.isNullOrEmpty(student.yiChengWei))
			str += "-第一监护人称谓";
		if(StringUtils.isNullOrEmpty(student.yiXingMing))
			str += "-第一监护人姓名";
		
		if(_instance.user.leiXing != 9)
		{
			if(_instance.user.leiXing != 1)
			{
				//幼儿园没有，学习简历 学籍状态  学籍号
				if(StringUtils.isNullOrEmpty(student.xueXiJianLi))
					str += "-学习简历";
				if(StringUtils.isNullOrEmpty(student.xueJiZhuangTai))
					str += "-学籍状态 ";
				if(StringUtils.isNullOrEmpty(student.xueJiHao))
					str += "-学籍号";
			}
			else
			{
				if(StringUtils.isNullOrEmpty(student.chuShengRiQi))
					str += "-出生日期";
			}
		}*/
		
		if(str != "")
		{
			str = student.xingMing + str;
			info = info + str + "\r\n";
		}
	}
	
	if(info != "")
	{
		FounderUtils.alert(info, "请核实", this, null, FounderUtils.faultIcon);
		btnSyn.enabled=false;
	}
	else
	{
		btnSyn.enabled=true;
	}
}

protected function btnSyn_clickHandler(event:MouseEvent):void
{
	if(_instance.user.leiXing == 9)
	{
		model.studentInfoQueryParameter.school=ddlSchool.selectedItem.sid;
	}
	else
	{
		model.studentInfoQueryParameter.school=_instance.user.xueXiaoId.toString();
	}
	model.studentInfoQueryParameter.insert=cb1.selected;
	model.studentInfoQueryParameter.iDelete=cb2.selected;
	model.studentInfoQueryParameter.update=cb3.selected;
	controller.synData(model.studentInfoQueryParameter, model.dataList);
}